using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using CMS.BIZ;
using System.Web.Security;

namespace CMS.Web.UI.UserControls
{
    public class MembershipUserEditor : UserControl
    {

        private BizMembershipUser user;

        protected TextBox tbSearch;
        protected Button bSearch;
        protected Repeater repeaterUser;

        protected Label lUserName;
        protected TextBox tbLastName;
        protected TextBox tbFirstName;
        protected DropDownList ddlCulture;
        protected TextBox tbImagePath;
        protected TextBox tbEmail;
        protected CheckBox cbLockedOut;
        protected CheckBox cbApproved;
        protected TextBox tbComment;
        protected Button bValidate;

        public MembershipUserEditor()
        {
            base.Init += new EventHandler(MembershipUserEditor_Init);
        }

        void MembershipUserEditor_Init(object sender, EventArgs e)
        {
            bValidate.Click += new EventHandler(Validate_Click);
            bSearch.Click += new EventHandler(Search_Click);
            string userName = Request.QueryString["UserName"];
            if (!string.IsNullOrEmpty(userName))
            {
                user = (BizMembershipUser)Membership.GetUser(userName, false);
            }
            if (!Page.IsPostBack && user != null)
            {
                BindCulture();
                lUserName.Text = user.UserName;
                tbEmail.Text = user.Email;
                tbFirstName.Text = user.FirstName;
                tbLastName.Text = user.LastName;
                cbApproved.Checked = user.IsApproved;
                cbLockedOut.Checked = user.IsLockedOut;
                tbComment.Text = user.Comment;
                if (tbImagePath != null) tbImagePath.Text = user.ImagePath;
                if (ddlCulture != null && !string.IsNullOrEmpty(user.CultureCode)) ddlCulture.SelectedValue = user.CultureCode;
            }
        }

        void Search_Click(object sender, EventArgs e)
        {
            repeaterUser.DataSource = Membership.FindUsersByName(tbSearch.Text);
            repeaterUser.DataBind();
        }

        private void BindCulture()
        {
            List<BizCulture> list = Page.BizCultureProvider.ReadAll();
            ddlCulture.DataSource = list;
            ddlCulture.DataValueField = "CultureCode";
            ddlCulture.DataTextField = "CultureName";
            ddlCulture.DataBind();
            ddlCulture.SelectedValue = Page.CultureCode;
        }

        void Validate_Click(object sender, EventArgs e)
        {
            if (user != null)
            {
                user.LastName = tbLastName.Text;
                user.FirstName = tbFirstName.Text;
                user.Email = tbEmail.Text;
                user.IsApproved = cbApproved.Checked;
                user.Comment = tbComment.Text;
                if (tbImagePath != null) user.ImagePath = tbImagePath.Text;
                if (ddlCulture != null) user.CultureCode = ddlCulture.SelectedValue;
                Membership.UpdateUser(user);
                if (user.IsLockedOut != cbLockedOut.Checked)
                {
                    if (user.IsLockedOut) user.UnlockUser();
                    else user.LockUser();
                }
            }
        }

    }
}
